package com.clz.latte.ec.main.sort;

import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.clz.latte.delegate.LatteDaleGate;
import com.clz.latte.ec.R;
import com.clz.latte.ec.entity.sort.VerticalListBean;
import com.clz.latte.ec.main.sort.content.ContentDelegate;

import me.yokeyword.fragmentation.SupportHelper;

/**
 * Created by Administrator on 2018/4/9.
 */

public class VerticalListRecAdapter extends BaseQuickAdapter<VerticalListBean, BaseViewHolder> {
    private int mSelectPosition;
    private final SortDelegate SORTDELEGATE;

    public VerticalListRecAdapter(int layoutResId, SortDelegate sortdelegate) {
        super(layoutResId);
        SORTDELEGATE = sortdelegate;
    }

    @Override
    protected void convert(BaseViewHolder holder, final VerticalListBean item) {
        final String text = item.getName();
        final AppCompatTextView name = holder.getView(R.id.tv_vertical_item_name);
        final View line = holder.getView(R.id.view_line);
        final View itemView = holder.itemView;
        name.setText(text);
        final int currentPos = holder.getAdapterPosition();
        if (mSelectPosition == currentPos) {
            line.setVisibility(View.VISIBLE);
            name.setTextColor(ContextCompat.getColor(mContext, R.color.app_main));
            line.setBackgroundColor(ContextCompat.getColor(mContext, R.color.app_main));
            itemView.setBackgroundColor(Color.WHITE);
        } else {
            line.setVisibility(View.INVISIBLE);
            name.setTextColor(ContextCompat.getColor(mContext, R.color.we_chat_black));
            itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.item_background));
        }
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notifyDataSetChanged();
                mSelectPosition = currentPos;
                final int contentId = item.getId();
                showContent(contentId);
            }
        });
    }

    private void showContent(int contentId){
        ContentDelegate contentDelegate=ContentDelegate.newInstance(contentId);
        switchContent(contentDelegate);
    }
    private void switchContent(ContentDelegate delegate){
        final LatteDaleGate contentDelegate =
                SupportHelper.findFragment(SORTDELEGATE.getChildFragmentManager(), ContentDelegate.class);
        if (contentDelegate != null) {
            contentDelegate.getSupportDelegate().replaceFragment(delegate, false);
        }
    }
}
